namespace SilverSea.Registration
{
    using System;
    using System.Security.Cryptography;
    using System.Text;

    public class CryptorEngine
    {
        public static string key = "@#$Le Hong Thai$#@";

        public static string ConvertToLetterDigit(string st)
        {
            StringBuilder builder = new StringBuilder();
            foreach (char ch in st)
            {
                if (!char.IsLetterOrDigit(ch))
                {
                    builder.Append(Convert.ToInt16(ch).ToString());
                }
                else
                {
                    builder.Append(ch);
                }
            }
            return builder.ToString();
        }

        public static string Decrypt(string cipherString, bool useHashing)
        {
            try
            {
                byte[] buffer;
                byte[] inputBuffer = Convert.FromBase64String(cipherString);
                if (useHashing)
                {
                    MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider();
                    buffer = provider.ComputeHash(Encoding.UTF8.GetBytes(key));
                    provider.Clear();
                }
                else
                {
                    buffer = Encoding.UTF8.GetBytes(key);
                }
                TripleDESCryptoServiceProvider provider2 = new TripleDESCryptoServiceProvider {
                    Key = buffer,
                    Mode = CipherMode.ECB,
                    Padding = PaddingMode.PKCS7
                };
                byte[] bytes = provider2.CreateDecryptor().TransformFinalBlock(inputBuffer, 0, inputBuffer.Length);
                provider2.Clear();
                return Encoding.UTF8.GetString(bytes);
            }
            catch
            {
                return "";
            }
        }

        public static string Encrypt(string toEncrypt, bool useHashing)
        {
            try
            {
                byte[] buffer;
                byte[] bytes = Encoding.UTF8.GetBytes(toEncrypt);
                if (useHashing)
                {
                    MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider();
                    buffer = provider.ComputeHash(Encoding.UTF8.GetBytes(key));
                    provider.Clear();
                }
                else
                {
                    buffer = Encoding.UTF8.GetBytes(key);
                }
                TripleDESCryptoServiceProvider provider2 = new TripleDESCryptoServiceProvider {
                    Key = buffer,
                    Mode = CipherMode.ECB,
                    Padding = PaddingMode.PKCS7
                };
                byte[] inArray = provider2.CreateEncryptor().TransformFinalBlock(bytes, 0, bytes.Length);
                provider2.Clear();
                return Convert.ToBase64String(inArray, 0, inArray.Length);
            }
            catch
            {
                return "";
            }
        }

        public static string InverseByBase(string st, int MoveBase)
        {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < st.Length; i += MoveBase)
            {
                int num;
                if ((i + MoveBase) > (st.Length - 1))
                {
                    num = st.Length - i;
                }
                else
                {
                    num = MoveBase;
                }
                builder.Append(InverseString(st.Substring(i, num)));
            }
            return builder.ToString();
        }

        public static string InverseString(string st)
        {
            StringBuilder builder = new StringBuilder();
            for (int i = st.Length - 1; i >= 0; i--)
            {
                builder.Append(st[i]);
            }
            return builder.ToString();
        }

        public static string RemoveUseLess(string st)
        {
            for (int i = st.Length - 1; i >= 0; i--)
            {
                char ch = char.ToUpper(st[i]);
                if (((ch < 'A') || (ch > 'Z')) && ((ch < '1') || (ch > '9')))
                {
                    st = st.Remove(i, 1);
                }
            }
            return st;
        }

        public static void SetKey(string securityKey)
        {
            if (!securityKey.Contains("Le Hong Thai"))
            {
                key = "Le Hong Thai " + securityKey;
            }
            else
            {
                key = securityKey;
            }
        }
    }
}

